package com.colortea.action.frontstage.index;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.colortea.action.backstage.base.DataCollectionTAction;
import com.colortea.action.backstage.utils.enums.BaseEnums;
import com.colortea.action.backstage.utils.enums.BaseEnums.GoodsState;
import com.colortea.action.frontstage.vo.RedisGoodsVo;
import com.colortea.entity.GoodsT;
import com.colortea.redis.service.RedisBaseTService;
import com.colortea.service.GoodsTService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("colortea")
@Namespace("")
@InterceptorRefs({ @InterceptorRef("defaultStack") })
public class IndexAction extends ActionSupport {
	@Resource
	private DataCollectionTAction dataCollectionTAction;
	@Resource
	private GoodsTService goodsTService;
	@Resource
	private RedisBaseTService redisBaseTService;
	
	private static final long serialVersionUID = 1L;
	/**
	 * 店铺id
	 */
	private String shopid;
	private int rp = 8;// 每页显示数量
	private int page = 1;
	private int total = 0;
	private int totalpage = 1;
	
	
	
	public String getShopid() {
		return shopid;
	}

	public void setShopid(String shopid) {
		this.shopid = shopid;
	}

	public int getRp() {
		return rp;
	}

	public void setRp(int rp) {
		this.rp = rp;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}

	

	/**
	 * 显示主页
	 * 
	 * @return
	 */
	@Action(value = "index", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/theme/default/shop/index.ftl") })
	public String index() {
		// 获取商城公众信息
		dataCollectionTAction.putBaseInfoToContext();
		RedisGoodsVo redisGoodsVo = (RedisGoodsVo)redisBaseTService.get(GoodsState.RECOMMENDED.getName(),RedisGoodsVo.class);
		if(redisGoodsVo == null){
			redisGoodsVo = new RedisGoodsVo();
			//镇店之宝
			Criterion criterion = Restrictions.and(Restrictions.eq("recommended",GoodsState.RECOMMENDED.getState()));
			List<GoodsT> goodsRecommendList = goodsTService.findByCriteriaByPage(GoodsT.class, criterion, Order.desc("createtime"), 1, 3);
			ActionContext.getContext().put("goodsrestrictionlist", goodsRecommendList);			
			redisGoodsVo.setGlist(goodsRecommendList);			
			//放入redis
			redisBaseTService.put(GoodsState.RECOMMENDED.getName(),redisGoodsVo,RedisGoodsVo.class,20,TimeUnit.MINUTES);
		}else{
			ActionContext.getContext().put("goodsrestrictionlist", redisGoodsVo.getGlist());
		}
		RedisGoodsVo redisGoodsVo2 = (RedisGoodsVo)redisBaseTService.get(GoodsState.NEW.getName(),RedisGoodsVo.class);
		if(redisGoodsVo2 == null){
			redisGoodsVo2 = new RedisGoodsVo();
			//新品上市
			Criterion criterion2 = Restrictions.and(Restrictions.eq("isNew",GoodsState.NEW.getState()));
			List<GoodsT> goodsNewList = goodsTService.findByCriteriaByPage(GoodsT.class, criterion2, Order.desc("createtime"), 1, 7);
			ActionContext.getContext().put("goodnewlist", goodsNewList);		
			redisGoodsVo2.setGlist(goodsNewList);
			//放入redis
			redisBaseTService.put(GoodsState.NEW.getName(),redisGoodsVo2,RedisGoodsVo.class,20,TimeUnit.MINUTES);
			
		}else{
			ActionContext.getContext().put("goodnewlist", redisGoodsVo2.getGlist());
		}
		RedisGoodsVo redisGoodsVo3 =  (RedisGoodsVo)redisBaseTService.get(GoodsState.HOTSALE.getName(),RedisGoodsVo.class);
		if(redisGoodsVo3 == null){
			redisGoodsVo3 = new RedisGoodsVo();
			//当季热卖
			Criterion criterion3 = Restrictions.and(Restrictions.eq("hotsale",GoodsState.HOTSALE.getState()));
			List<GoodsT> goodsHotsaleList = goodsTService.findByCriteriaByPage(GoodsT.class, criterion3, Order.desc("createtime"), 1, 8);
			ActionContext.getContext().put("goodshotsaleList", goodsHotsaleList);		
			redisGoodsVo3.setGlist(goodsHotsaleList);
			//放入redis
			redisBaseTService.put(GoodsState.HOTSALE.getName(),redisGoodsVo3,RedisGoodsVo.class,20,TimeUnit.MINUTES);			
		}else{
			ActionContext.getContext().put("goodshotsaleList", redisGoodsVo3.getGlist());		
		}		
		return SUCCESS;
	}

	
	
}
